package com.tracemap.websender.xmlparser;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;


public class EmployeeRepository
{
    public static ArrayList<EmployeeXML> loadMany(String url)
    {
        int parserEvent = -1;
        XmlPullParser parser = null;
        ArrayList<EmployeeXML> items = new ArrayList<EmployeeXML>();
        String tag = "";
        EmployeeXML item = null;

        try {
            // calls service (referenced in url) to request XML serialized data
            parser = XmlHelper.loadData(url);
            parserEvent = parser.getEventType();

            while (parserEvent != XmlPullParser.END_DOCUMENT) {
                switch(parserEvent) {
                  case XmlPullParser.START_TAG:
                      tag = parser.getName();
                      if (tag.compareTo("Employee") == 0){
                          item = new EmployeeXML();
                      }
                  break;
                  case XmlPullParser.END_TAG:
                      tag = parser.getName();
                      if (tag.compareTo("Employee") == 0){
                          items.add(item);
                      }
                  break;
                  case XmlPullParser.TEXT:
                      String text = parser.getText();
                      if (text.trim().length() == 0) break;

                      if (tag.compareTo("Id") == 0){
                          item.Id = Integer.parseInt(text);
                      }
                      else if (tag.compareTo("DeptId") == 0){
                          item.DeptId = Integer.parseInt(text);
                      }
                      else if (tag.compareTo("SalaryGradeId") == 0){
                          item.SalaryGradeId = Integer.parseInt(text);
                      }
                      else if(tag.compareTo("FirstName") == 0){
                          item.FirstName = text;
                      }
                      else if(tag.compareTo("LastName") == 0){
                          item.LastName = text;
                      }
                      else if(tag.compareTo("Email") == 0){
                          item.Email = text;
                      }
                      else if(tag.compareTo("Phone") == 0){
                          item.Phone = text;
                      }
                      else if(tag.compareTo("Title") == 0){
                          item.Title = text;
                      }
                      else if(tag.compareTo("TakesLongLunches") == 0){
                          item.TakesLongLunches = Boolean.parseBoolean(text);
                      }

                  break;
                }

                parserEvent = parser.next();
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // return de-serialized list of employees
        return items;
    }
}